﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunOfAlgorithms.Chapter03
{
    public class GoogleX
    {
        public void Main()
        {
            TagCharItem[] tagCharItem =
            {
                new TagCharItem() { C = 'W', Value = -1, Leading = true },//
                new TagCharItem() { C = 'D', Value = -1, Leading = true },//
                new TagCharItem() { C = 'O', Value = -1, Leading = false },
                new TagCharItem() { C = 'T', Value = -1, Leading = false },
                new TagCharItem() { C = 'G', Value = -1, Leading = true },//
                new TagCharItem() { C = 'L', Value = -1, Leading = false },
                new TagCharItem() { C = 'E', Value = -1, Leading = false },
                new TagCharItem() { C = 'C', Value = -1, Leading = false },
                new TagCharItem() { C = 'M', Value = -1, Leading = false }
            };

            TagCharValue[] tagCharValues =
            {
                new TagCharValue(){ Used = false, Value = 0},
                new TagCharValue(){ Used = false, Value = 1},
                new TagCharValue(){ Used = false, Value = 2},
                new TagCharValue(){ Used = false, Value = 3},
                new TagCharValue(){ Used = false, Value = 4},
                new TagCharValue(){ Used = false, Value = 5},
                new TagCharValue(){ Used = false, Value = 6},
                new TagCharValue(){ Used = false, Value = 7},
                new TagCharValue(){ Used = false, Value = 8},
                new TagCharValue(){ Used = false, Value = 9},
            };

            SearchingResult(tagCharItem, tagCharValues, 0);
        }


        /// <summary>
        /// 使用递归方式进行组合枚举
        /// </summary>
        /// <param name="tagCharItems"></param>
        /// <param name="tagCharValues"></param>
        /// <param name="index"></param>
        public void SearchingResult(TagCharItem[] tagCharItems, TagCharValue[] tagCharValues, int index)
        {
            if (index == tagCharItems.Length)
            {
                IsOK(tagCharItems);
                return;
            }

            for (int i = 0; i < tagCharValues.Length; i++)
            {
                if (IsValueValid(tagCharItems[index], tagCharValues[i]))
                {
                    tagCharValues[i].Used = true;
                    tagCharItems[index].Value = tagCharValues[i].Value;

                    SearchingResult(tagCharItems, tagCharValues, index + 1);

                    tagCharValues[i].Used = false;
                }
            }
        }

        /// <summary>
        /// 判定数据是否有效，是否可以被赋值
        /// </summary>
        /// <param name="tagCharItem"></param>
        /// <param name="tagCharValue"></param>
        /// <returns></returns>
        public bool IsValueValid(TagCharItem tagCharItem, TagCharValue tagCharValue)
        {
            if (tagCharItem.Leading)
            {
                if (!tagCharValue.Used)
                {
                    if (tagCharValue.Value == 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (!tagCharValue.Used)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        /// <summary>
        /// 判定现有数据是否符合题目要求
        /// </summary>
        /// <param name="tagCharItems"></param>
        public void IsOK(TagCharItem[] tagCharItems)
        {
            string WWWDOT = "WWWDOT";
            ReplacaMethods(ref WWWDOT, tagCharItems);
            int _WWWDOT = Convert.ToInt32(WWWDOT);

            string GOOGLE = "GOOGLE";
            ReplacaMethods(ref GOOGLE, tagCharItems);
            int _GOOGLE = Convert.ToInt32(GOOGLE);

            string DOTCOM = "DOTCOM";
            ReplacaMethods(ref DOTCOM, tagCharItems);
            int _DOTCOM = Convert.ToInt32(DOTCOM);

            if (_WWWDOT - _GOOGLE == _DOTCOM)
            {
                Console.WriteLine($"WWWDOT = {WWWDOT},GOOGLE = {GOOGLE},DOTCOM = {DOTCOM}");
            }
        }


        public void ReplacaMethods(ref string Example, TagCharItem[] tagCharItems)
        {
            for (int i = 0; i < tagCharItems.Length; i++)
            {
                Example = Example.Replace(tagCharItems[i].C.ToString(), tagCharItems[i].Value.ToString());
            }
        }
    }

    public class TagCharItem
    {
        public char C;
        public int Value;
        public bool Leading;
    }
    public class TagCharValue
    {
        public bool Used;
        public int Value;
    }
}
